package com.example.smallmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.smallmall.dto.SpOrderQueryParam;
import com.example.smallmall.entity.SpOrder;
import com.example.smallmall.mapper.SpOrderMapper;
import com.example.smallmall.service.ISpOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author WSGL
 * @since 2020-09-17
 */
@Service
public class SpOrderServiceImpl extends ServiceImpl<SpOrderMapper, SpOrder> implements ISpOrderService {

    @Resource
    private SpOrderMapper spOrderMapper;

    @Override
    public Map<String, Object> selectByPage(SpOrderQueryParam queryParam, Integer pageNum, Integer pageSize) {
        Map<String, Object> objectMap = new HashMap<>();
        Page<SpOrder> orderPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SpOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (queryParam != null) {
            if (queryParam.getUserId() != null) {
                lambdaQueryWrapper.eq(SpOrder::getUserId, queryParam.getUserId());
            } else if (queryParam.getPayStatus() != null) {
                lambdaQueryWrapper.eq(SpOrder::getPayStatus, queryParam.getPayStatus());
            } else if (queryParam.getIsSend() != null) {
                lambdaQueryWrapper.eq(SpOrder::getIsSend, queryParam.getIsSend());
            } else if (queryParam.getOrderFapiaoTitle() != null) {
                lambdaQueryWrapper.like(SpOrder::getOrderFapiaoTitle, queryParam.getOrderFapiaoTitle());
            } else if (queryParam.getOrderFapiaoCompany() != null) {
                lambdaQueryWrapper.like(SpOrder::getOrderFapiaoCompany, queryParam.getOrderFapiaoCompany());
            } else if (queryParam.getOrderFapiaoContent() != null) {
                lambdaQueryWrapper.like(SpOrder::getOrderFapiaoContent, queryParam.getOrderFapiaoContent());
            } else if (queryParam.getConsigneeAddr() != null) {
                lambdaQueryWrapper.like(SpOrder::getConsigneeAddr, queryParam.getConsigneeAddr());
            }
        }
        IPage<SpOrder> spOrderPage = spOrderMapper.selectPage(orderPage, lambdaQueryWrapper.orderByDesc(SpOrder::getCreateTime));
        objectMap.put("orders", spOrderPage.getRecords());
        objectMap.put("pageNum", spOrderPage.getCurrent());
        objectMap.put("total", spOrderPage.getTotal());
        if (!objectMap.isEmpty()) {
            return objectMap;
        } else {
            return null;
        }
    }
}
